﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	public partial class HotWaterTypes : System.Windows.Forms.Form
	{
		public HotWaterTypes()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = (base.Icon = Resource.YArchitechIcon);
			this.InitListView();
			base.CancelButton = this.buttonCancel;
		}

		private void InitListView()
		{
			this.UsedWaterTypes.Columns.Add("", 0);
			if (HotUsedWaterCalManage.Instance.bIsCalForDay)
			{
				this.UsedWaterTypes.Columns.Add("建筑物名称", 205, HorizontalAlignment.Left);
				this.UsedWaterTypes.Columns.Add("最高日用水定额(L)", 120, HorizontalAlignment.Center);
				this.UsedWaterTypes.Columns.Add("单位", 100, HorizontalAlignment.Center);
				this.UsedWaterTypes.Columns.Add("用水时间(h)", 90, HorizontalAlignment.Center);
			}
			else
			{
				this.PrivateBuilding.Text = "住宅、旅馆、别墅、宾馆、酒店式公寓";
				this.PublicBuilding.Text = "宿舍、招待所、培训中心";
				this.OtherBuilding.Text = "其它";
				this.TimeUsed.Text = "使用水温：";
				this.UsedWaterTypes.Columns.Add("卫生器具名称", 205, HorizontalAlignment.Left);
				this.UsedWaterTypes.Columns.Add("一次用水量", 120, HorizontalAlignment.Center);
				this.UsedWaterTypes.Columns.Add("小时用水量", 100, HorizontalAlignment.Center);
				this.UsedWaterTypes.Columns.Add("使用水温", 90, HorizontalAlignment.Center);
			}
			this.UsedWaterTypes.ItemSelectionChanged += this.UsedWaterTypes_ItemSelectionChanged;
			this.UsedWaterTypes.DoubleClick += this.UsedWaterTypes_DoubleClick;
			this.PrivateBuilding.Checked = true;
		}

		private void UsedWaterTypes_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
		{
			if (e.IsSelected && e.Item.Tag != null)
			{
				HotUsedWaterCal hotUsedWaterCal = (HotUsedWaterCal)e.Item.Tag;
				this.MaxUsedWaterValue.Text = hotUsedWaterCal.maxUsedProposal.ToString();
				this.UsedCountValue.Text = hotUsedWaterCal.usedCount.ToString();
				if (HotUsedWaterCalManage.Instance.bIsCalForDay)
				{
					this.UsedTimeValue.Text = hotUsedWaterCal.usedTimeProposal.ToString();
					return;
				}
				this.UsedTimeValue.Text = hotUsedWaterCal.usedTempProposal.ToString();
			}
		}

		private void UsedWaterTypes_DoubleClick(object sender, EventArgs e)
		{
			this.Apply();
		}

		private void PrivateHouse_CheckedChanged(object sender, EventArgs e)
		{
			this.FillList(WaterCalculateType.PrivateBuilding);
		}

		private void PublicHouse_CheckedChanged(object sender, EventArgs e)
		{
			this.FillList(WaterCalculateType.PublicBuilding);
		}

		private void OtherHouse_CheckedChanged(object sender, EventArgs e)
		{
			this.FillList(WaterCalculateType.OtherBuilding);
		}

		private void FillList(WaterCalculateType type)
		{
			this.UsedWaterTypes.Items.Clear();
			if (HotUsedWaterCalManage.Instance.bIsCalForDay)
			{
				using (List<HotUsedWaterCal>.Enumerator enumerator = HotUsedWaterCalManage.Instance.GetHotDayCalArray.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						HotUsedWaterCal hotUsedWaterCal = enumerator.Current;
						if (hotUsedWaterCal.calculateType == type)
						{
							ListViewItem listViewItem = new ListViewItem();
							listViewItem.Tag = new HotUsedWaterCal(hotUsedWaterCal);
							listViewItem.Text = "";
							ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
							listViewSubItem.Text = hotUsedWaterCal.typeName;
							listViewItem.SubItems.Add(listViewSubItem);
							listViewSubItem = new ListViewItem.ListViewSubItem();
							listViewSubItem.Text = hotUsedWaterCal.maxUsedRange;
							listViewItem.SubItems.Add(listViewSubItem);
							listViewSubItem = new ListViewItem.ListViewSubItem();
							listViewSubItem.Text = hotUsedWaterCal.unit;
							listViewItem.SubItems.Add(listViewSubItem);
							listViewSubItem = new ListViewItem.ListViewSubItem();
							listViewSubItem.Text = hotUsedWaterCal.usedTimeRange;
							listViewItem.SubItems.Add(listViewSubItem);
							this.UsedWaterTypes.Items.Add(listViewItem);
						}
					}
					return;
				}
			}
			foreach (HotUsedWaterCal hotUsedWaterCal2 in HotUsedWaterCalManage.Instance.GetHotHourCalArray)
			{
				if (hotUsedWaterCal2.calculateType == type)
				{
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Tag = new HotUsedWaterCal(hotUsedWaterCal2);
					listViewItem.Text = "";
					ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = hotUsedWaterCal2.typeName;
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = hotUsedWaterCal2.oneUsedRange;
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = hotUsedWaterCal2.maxUsedRange;
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = hotUsedWaterCal2.usedTempRange;
					listViewItem.SubItems.Add(listViewSubItem);
					this.UsedWaterTypes.Items.Add(listViewItem);
				}
			}
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			this.Apply();
		}

		private void Apply()
		{
			if (this.UsedWaterTypes.SelectedItems.Count > 0)
			{
				bool flag = false;
				try
				{
					ListViewItem listViewItem = this.UsedWaterTypes.SelectedItems[0];
					if (listViewItem.Tag != null)
					{
						this.calculate = new HotUsedWaterCal((HotUsedWaterCal)listViewItem.Tag);
						this.calculate.maxUsedProposal = Convert.ToDouble(this.MaxUsedWaterValue.Text);
						this.calculate.usedCount = Convert.ToDouble(this.UsedCountValue.Text);
						if (HotUsedWaterCalManage.Instance.bIsCalForDay)
						{
							this.calculate.usedTimeProposal = Convert.ToDouble(this.UsedTimeValue.Text);
						}
						else
						{
							this.calculate.usedTempProposal = Convert.ToDouble(this.UsedTimeValue.Text);
						}
						flag = true;
					}
				}
				catch (FormatException ex)
				{
					ex.ToString();
					MessageBox.Show(this, "请输入一个有效的数字！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
				catch (OverflowException ex2)
				{
					ex2.ToString();
					MessageBox.Show(this, "请输入一个有效的数字！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
				catch (Exception ex3)
				{
					ex3.ToString();
				}
				if (flag)
				{
					base.Close();
				}
			}
		}

		public HotUsedWaterCal calculate;
	}
}
